package com.tenebrous.jestic.controller.framwork.meber.organ;

import com.tenebrous.jestic.compents.annotations.Weblog;
import com.tenebrous.jestic.compents.annotations.WeblogEnum;
import com.tenebrous.jestic.controller.SupperController;
import com.tenebrous.jestic.entity.TreeSerializable;
import com.tenebrous.jestic.entity.framwork.meber.organ.OrgOrganize;
import com.tenebrous.jestic.entity.framwork.meber.organ.OrgUser;
import com.tenebrous.jestic.entity.framwork.system.SysLogModify;
import com.tenebrous.jestic.service.framwork.meber.organ.OrgOrganizeService;
import com.rain.ops.sdk.dto.ApiResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

@Validated
@RestController
@RequestMapping("/organ/organize")
public class OrganizeController extends SupperController {

    @Autowired
    private OrgOrganizeService organizeService;

    /**
     * 查找组织记录
     *
     * @param id
     * @return
     */
    @PostMapping("/get")
    public ApiResult get(@NotBlank String id) {
        OrgOrganize oitem = organizeService.selectByPrimaryKey(id);
        if (oitem != null) {
            return new ApiResult(200, "查询成功。", oitem);
        }
        return new ApiResult(999, "查询失败，未找到指定对象。");
    }

    /**
     * 查找组织记录，包含关联信息
     *
     * @param id
     * @return
     */
    @PostMapping("/load")
    public ApiResult load(@NotBlank String id) {
        OrgOrganize oitem = organizeService.getById(id);
        if (oitem != null) {
            return new ApiResult(200, "查询成功。", oitem);
        }
        return new ApiResult(999, "查询失败，未找到指定对象。", new OrgOrganize());
    }

    /**
     * 组织分页查询
     *
     * @param cid
     * @param pid
     * @param name
     * @return
     */
    @PostMapping("/paging")
    public ApiResult paging(String cid, String pid, String name) {
        return new ApiResult(200, "查询成功。", organizeService.queryPagingList(super.getPageIndex(), super.getPageSize(), cid, pid, name));
    }

    @PostMapping("/query")
    public ApiResult query(String[] cids, String[] otypes) {
        return new ApiResult(200, "查询成功。", organizeService.queryList(cids, otypes));
    }

    /**
     * 查询机构树
     *
     * @param cid    单位ID
     * @param otypes 机构类型
     * @return
     */
    @PostMapping("/query-tree")
    public ApiResult queryTree(String cid, String[] otypes, boolean extended) {
        return new ApiResult(200, "查询成功。", organizeService.queryTree(cid, otypes, extended));
    }

    /**
     * 查询部门组织树
     *
     * @param cid
     * @return
     */
    @PostMapping("/query-dtree")
    public ApiResult queryDtree(@NotBlank String cid) {
        List<TreeSerializable> olist = organizeService.queryDtree(cid, null, false);
        return new ApiResult(200, "查询成功。", olist);
    }

    /**
     * 查询机构数 所有
     *
     * @param otypes
     * @param extended
     * @return
     */
    @PostMapping("/query-alltree")
    public ApiResult queryAlltree(String[] otypes, boolean extended) {
        String[] companyids = null;
        return new ApiResult(200, "查询成功。", organizeService.queryTree(companyids, otypes, extended));
    }

    @PostMapping("/query-chart-tree")
    public ApiResult queryChartTree(String cid) {
        String[] companyids = new String[]{};
        if (StringUtils.isNotBlank(cid)) {
            companyids = new String[]{cid};
        }
        return new ApiResult(200, "查询成功。", organizeService.queryChartTree(companyids));
    }

    /**
     * 查询机构树
     *
     * @param ograde 类型
     * @return
     */
    @PostMapping("/queryOrgByOgrade")
    public ApiResult queryOrgByOgrade(String ograde) {
        List<OrgOrganize> list = new ArrayList<>();
        if (StringUtils.isNotBlank(ograde)) {
            list = organizeService.queryOrgByOgrade(ograde);
        }
        return new ApiResult(200, "查询成功。", list);
    }

    /**
     * 更新对象
     *
     * @param item
     * @return
     */
    @PostMapping("/update")
    @Weblog(description = "更新组织机构信息", method = WeblogEnum.UPDATE)
    public ApiResult update(@NotNull OrgOrganize item) {
        OrgUser ouser = super.getCurrentUser();
        return new ApiResult(200, "更新成功。", organizeService.update(item, ouser));
    }

    /**
     * 向上移动
     *
     * @param id
     * @return
     */
    @PostMapping("/up")
    @Weblog(description = "组织机构位置上移", method = WeblogEnum.UPDATE)
    public ApiResult up(@NotBlank String id) {
        return organizeService.up(id);
    }

    /**
     * 向下移动
     *
     * @param id
     * @return
     */
    @PostMapping("/down")
    @Weblog(description = "组织机构位置下移", method = WeblogEnum.UPDATE)
    public ApiResult down(@NotBlank String id) {
        return organizeService.down(id);
    }

    /**
     * 删除指定记录及其下级所有组织信息
     *
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    @Weblog(description = "删除指定组织机构及其下级所有组织", method = WeblogEnum.DELETE)
    public ApiResult delete(@NotEmpty String[] ids) {
        return new ApiResult(200, "删除成功。", organizeService.deleteTree(ids));
    }

    @Weblog(description = "删除所有组织机构信息", method = WeblogEnum.UPDATE)
    @PostMapping("/deleteall")
    public ApiResult deleteall(String did, String companyid, String otype, String code) {
        if ("OD".equals(otype)) {
            organizeService.deleteall(code, null);
        } else if ("OG".equals(otype)) {
            organizeService.deleteall(null, did);
        }
        return new ApiResult(200, "删除成功。");
    }

    /**
     * 移动
     *
     * @param id               被移动的部门ID
     * @param targetOrganizeid 移动后所属的部门ID
     * @return
     */
    @PostMapping("/move")
    @Weblog(description = "组织机构移动变更", method = WeblogEnum.UPDATE)
    public ApiResult move(@NotBlank String id, @NotBlank String targetOrganizeid, @NotNull SysLogModify mlog) {
        OrgUser ouser = super.getCurrentUser();
        return organizeService.move(id, targetOrganizeid, mlog, ouser);
    }

    /**
     * 组织拆分
     *
     * @param id       被拆分的部门ID
     * @param retained 是否保留被拆分的部门
     * @param items    拆分后的部门字符串
     * @return
     */
    @PostMapping("/split")
    @Weblog(description = "组织机构拆分变更", method = WeblogEnum.UPDATE)
    public ApiResult split(@NotBlank String id, @NotNull Boolean retained, @NotBlank String items, @NotNull SysLogModify mlog) {
        OrgUser ouser = super.getCurrentUser();
        return organizeService.split(id, retained, items, mlog, ouser);
    }

    /**
     * 合并组织
     *
     * @param ids  被合并的组织ID集合
     * @param item 合并后的组织信息
     * @return
     */
    @PostMapping("/meger")
    @Weblog(description = "组织机构合并变更", method = WeblogEnum.UPDATE)
    public ApiResult meger(@NotNull String[] ids, @NotNull OrgOrganize item, @NotNull SysLogModify mlog) {
        OrgUser ouser = super.getCurrentUser();
        return organizeService.meger(ids, item, mlog, ouser);
    }

    /**
     * 组织更名
     *
     * @param id   被更名的组织ID
     * @param item 更名后的组织信息
     * @return
     */
    @PostMapping("/rename")
    @Weblog(description = "组织机构重命名", method = WeblogEnum.UPDATE)
    public ApiResult rename(@NotBlank String id, @NotNull OrgOrganize item, @NotNull SysLogModify mlog) {
        OrgUser ouser = super.getCurrentUser();
        return organizeService.rename(id, item, mlog, ouser);
    }

    /**
     * 撤销组织
     *
     * @param id   被撤销的组织ID
     * @param mlog
     * @return
     */
    @PostMapping("/cancel")
    @Weblog(description = "组织机构撤销变更", method = WeblogEnum.UPDATE)
    public ApiResult cancel(@NotBlank String id, @NotNull SysLogModify mlog) {
        OrgUser ouser = super.getCurrentUser();
        return organizeService.cancel(id, mlog, ouser);
    }
}